package com.yunhe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhe.common.QueryPageParam;
import com.yunhe.common.Result;
import com.yunhe.entity.Menu;
import com.yunhe.entity.User;
import com.yunhe.service.MenuService;
import com.yunhe.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
/**
 * 控制层的类：调用业务层；
 * #注意：登录之后，去拿刚才获取的菜单；
 */
@RestController
@RequestMapping("/user")
@Api(value = "/user",description = "用户信息管理")
public class UserController {
    @Autowired
    MenuService menuService;
    @Autowired
    UserService userService;

    /**
     * 查找所有用户列表的测试代码
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取所有的用户信息",notes = "获取所有用户列表notes")
    public List<User> getUserList(){
        List<User> userList = userService.list();
        return userList;
    }

    @PostMapping("/login")
    @ApiOperation("根据账号和密码登录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功"),
            @ApiResponse(code = 400,message = "登录失败")
    })
    /**
     * 登录这里可以改：String no,String password
     */
    public Result login(
            @ApiParam("登录表单的账号和密码")
            @RequestBody User user){
        List<User> userList = userService.lambdaQuery().eq(User::getNo, user.getNo()).eq(User::getPassword, user.getPassword()).list();

        if(userList.size()>0){
            //这个user1，是登录成功之后，获取成功的实体类的对象
            //上面参数user之后：账号和密码，没有其他资源
            User user1= (User)userList.get(0);
            //获取菜单
            List<Menu> menuList = menuService.lambdaQuery().like(Menu::getMenuright,user1.getRoleId()).list();

            //如何将user和菜单列表发送给前端
            HashMap res=new HashMap();
            res.put("user",user1);
            res.put("menu", menuList);

            return Result.success(res);
        }
        return Result.fail();
    }
    @GetMapping("/findByNo")
    @ApiOperation("查询用户账号是否存在")
    public Result findByNo(
            @ApiParam("用户账号")
            @RequestParam String no){
        List<User> userList = userService.lambdaQuery().eq(User::getNo, no).list();
        return userList.size()>0?Result.success():Result.fail();
    }

    @PostMapping("/save")
    @ApiOperation("保存用户信息")
    public Result save(
            @ApiParam("用户表单信息")
            @RequestBody User user){
        boolean result = userService.save(user);
        if(result)
            return Result.success();
        return Result.fail();
    }

    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    public Result update(
            @ApiParam("用户表单信息")
            @RequestBody User user){
        return userService.updateById(user)?Result.success():Result.fail();
    }
    @DeleteMapping("/del")
    @ApiOperation("根据用户id来删除用户信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除信息成功"),
            @ApiResponse(code = 400,message = "删除信息失败")
    })
    @ApiImplicitParam(paramType = "query",dataType = "int",name = "id",value = "用户id",required = true)
    public Result del(@ApiParam("待删除的用户编号")Integer id){
         return userService.removeById(id)?Result.success():Result.fail();
    }

    @PostMapping("/listPageC1")
    @ApiOperation("分页查询用户信息")
    public Result listPageC1(
            @ApiParam("分页查询信息")
            @RequestBody QueryPageParam query){
        //3个param的封装数据
        HashMap param= query.getParam("param");
        //从HashMap对象param获取具体的三个值
        String name= (String) param.get("name");
        String sex= (String) param.get("sex");
        String roleId= (String) param.get("roleId");

        //下面要做分页的查询;
        //定义分页对象;
        Page<User>page = new Page<>();
        //设置当前的页码：就是第几页?
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        //接下来利用lambda表达式查询;Alt+Enter键，自动出来前面的变量
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //姓名是模糊查询
        if(StringUtils.isNotBlank(name)&&!"null".equals(name))
            lambdaQueryWrapper.like(User::getName, name);
        if(StringUtils.isNotBlank(sex))
            lambdaQueryWrapper.eq(User::getSex, sex);
        if(StringUtils.isNotBlank(roleId))
            lambdaQueryWrapper.eq(User::getRoleId, roleId);

        //调用原生查询代码;
        Page<User> result = userService.page(page, lambdaQueryWrapper);

        return Result.success(result.getRecords(),result.getTotal());
    }
}